/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service.ask;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;
import com.boxin.ims.modules.wechat.entity.ask.AskAnwser;
import com.boxin.ims.modules.wechat.dao.ask.AskAnwserDao;

/**
 * 疑问解答Service
 * @author Jakemanse
 * @version 2014-01-03
 */
@Component
@Transactional(readOnly = true)
public class AskAnwserService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(AskAnwserService.class);
	
	@Autowired
	private AskAnwserDao askAnwserDao;
	
	public AskAnwser get(Long id) {
		return askAnwserDao.findOne(id);
	}
	
	public Page<AskAnwser> find(Page<AskAnwser> page, AskAnwser askAnwser) {
		DetachedCriteria dc = askAnwserDao.createDetachedCriteria();
		if (askAnwser.getUser()!=null && askAnwser.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", askAnwser.getUser().getId()));
		}
		
		if (StringUtils.isNotEmpty(askAnwser.getName())){
			dc.add(Restrictions.like("name", "%"+askAnwser.getName()+"%"));
		}
		
		dc.addOrder(Order.desc("sort"));
		return askAnwserDao.find(page, dc);
	}
	
	
	
	@Transactional(readOnly = false)
	public void save(AskAnwser askAnwser) {
		
		if(com.thinkgem.jeesite.common.utils.StringUtils.isNotEmpty(askAnwser.getContent())){
			askAnwser.setContent(StringEscapeUtils.unescapeHtml4(askAnwser.getContent()));
		}
		
		askAnwserDao.save(askAnwser);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		askAnwserDao.deleteById(id);
	}
	
}
